﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandLineBuilder.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2013
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CommandlineHandling
{
    using System.Text;

    public class CommandLineBuilder : ICommandLineBuilder
    {
        public CommandLineBuilder()
        {
        }

        public string GetCommandLineString(ICommandLineOption option)
        {
            ExceptionExtensions.ThrowArgumentNullException(option, () => option);

            var sb = new StringBuilder();
            sb.Append(string.Format("/{0}", option.Name));
            if (string.IsNullOrEmpty(option.Value) == false)
            {
                sb.Append(string.Format(":{0}", option.Value));
            }

            if (option.Parameters.Count > 0)
            {
                foreach (OptionParameter parameter in option.Parameters)
                {
                    sb.Append(string.Format(" -{0}", parameter.Name));
                    string paramValue = parameter.Value;
                    if (string.IsNullOrEmpty(paramValue) == false)
                    {
                        sb.Append(string.Format(":\"{0}\"", paramValue));
                    }
                }
            }

            if (option.Next != null)
            {
                sb.Append(" " + this.GetCommandLineString(option.Next));
            }

            return sb.ToString().Trim();
        }
    }
}